import http from '@/utils/http'
import { mockUserInfo, mockVerifyCodes, mockWxLoginData, mockDelay, mockApiResponse } from '@/api/goods/mock'

// 是否使用模拟数据（开发时使用）
const USE_MOCK_DATA = false

/**
 * 用户登录
 * @param {Object} loginData - 登录数据
 */
export async function userLogin(loginData) {
  try {
    const result = await http.post('/user/login', loginData)
    return result
  } catch (error) {
    console.error('用户登录失败:', error)
    throw error
  }
}

/**
 * 用户注册
 * @param {Object} registerData - 注册数据
 */
export async function userRegister(registerData) {
  try {
    const result = await http.post('/user/register', registerData)
    return result
  } catch (error) {
    console.error('用户注册失败:', error)
    throw error
  }
}

/**
 * 获取用户信息
 */
export async function getUserInfo() {
  if(USE_MOCK_DATA){
    await mockDelay(500)
    return mockApiResponse(mockUserInfo)
  }
  
  try {
    const result = await http.get('/user/info')
    return result
  } catch (error) {
    console.error('获取用户信息失败:', error)
    throw error
  }
}

/**
 * 更新用户信息
 * @param {Object} userData - 用户数据
 */
export async function updateUserInfo(userData) {
  try {
    const result = await http.put('/user/info', userData)
    return result
  } catch (error) {
    console.error('更新用户信息失败:', error)
    throw error
  }
}

/**
 * 修改密码
 * @param {Object} passwordData - 密码数据
 */
export async function changePassword(passwordData) {
  try {
    const result = await http.put('/user/password', passwordData)
    return result
  } catch (error) {
    console.error('修改密码失败:', error)
    throw error
  }
}

/**
 * 忘记密码
 * @param {Object} resetData - 重置密码数据
 */
export async function forgotPassword(resetData) {
  try {
    const result = await http.post('/user/forgot-password', resetData)
    return result
  } catch (error) {
    console.error('忘记密码失败:', error)
    throw error
  }
}

/**
 * 重置密码
 * @param {Object} resetData - 重置密码数据
 */
export async function resetPassword(resetData) {
  try {
    const result = await http.post('/user/reset-password', resetData)
    return result
  } catch (error) {
    console.error('重置密码失败:', error)
    throw error
  }
}

/**
 * 发送验证码
 * @param {Object} codeData - 验证码数据
 */
export async function sendVerificationCode(codeData) {
  try {
    const result = await http.post('/user/send-code', codeData)
    return result
  } catch (error) {
    console.error('发送验证码失败:', error)
    throw error
  }
}

/**
 * 验证验证码
 * @param {Object} verifyData - 验证数据
 */
export async function verifyCode(verifyData) {
  try {
    const result = await http.post('/user/verify-code', verifyData)
    return result
  } catch (error) {
    console.error('验证验证码失败:', error)
    throw error
  }
}

/**
 * 用户登出
 */
export async function userLogout() {
  try {
    const result = await http.post('/user/logout')
    return result
  } catch (error) {
    console.error('用户登出失败:', error)
    throw error
  }
}

/**
 * 获取用户统计信息
 */
export async function getUserStats() {
  try {
    const result = await http.get('/user/stats')
    return result
  } catch (error) {
    console.error('获取用户统计失败:', error)
    throw error
  }
}

/**
 * 获取用户收藏列表
 * @param {Object} params - 查询参数
 */
export async function getUserFavorites(params = {}) {
  const { page = 1, pageSize = 10 } = params
  
  try {
    const result = await http.get('/user/favorites', {
      page,
      pageSize
    })
    return result
  } catch (error) {
    console.error('获取用户收藏失败:', error)
    throw error
  }
}

/**
 * 添加收藏
 * @param {Object} favoriteData - 收藏数据
 */
export async function addFavorite(favoriteData) {
  try {
    const result = await http.post('/user/favorites', favoriteData)
    return result
  } catch (error) {
    console.error('添加收藏失败:', error)
    throw error
  }
}

/**
 * 取消收藏
 * @param {string|number} goodsId - 商品ID
 */
export async function removeFavorite(goodsId) {
  try {
    const result = await http.delete(`/user/favorites/${goodsId}`)
    return result
  } catch (error) {
    console.error('取消收藏失败:', error)
    throw error
  }
}

/**
 * 检查是否已收藏
 * @param {string|number} goodsId - 商品ID
 */
export async function checkFavorite(goodsId) {
  try {
    const result = await http.get(`/user/favorites/check/${goodsId}`)
    return result
  } catch (error) {
    console.error('检查收藏状态失败:', error)
    throw error
  }
}

/**
 * 获取微信登录配置
 */
export async function getWxLoginConfig() {
  if(USE_MOCK_DATA){
    await mockDelay(500)
    
    return mockApiResponse({
      appId: 'mock_app_id',
      timestamp: Date.now(),
      nonceStr: 'mock_nonce_str',
      signature: 'mock_signature'
    })
  }
  
  try {
    const result = await http.get('/user/wx-login-config')
    return result
  } catch (error) {
    console.error('获取微信登录配置失败:', error)
    throw error
  }
}

/**
 * 微信登录
 * @param {Object} wxLoginData - 微信登录数据
 */
export async function wxLogin(wxLoginData) {
  try {
    const result = await http.post('/user/wx-login', wxLoginData)
    return result
  } catch (error) {
    console.error('微信登录失败:', error)
    throw error
  }
}

/**
 * 绑定微信
 * @param {Object} bindData - 绑定数据
 */
export async function bindWechat(bindData) {
  try {
    const result = await http.post('/user/bind-wechat', bindData)
    return result
  } catch (error) {
    console.error('绑定微信失败:', error)
    throw error
  }
}

/**
 * 解绑微信
 */
export async function unbindWechat() {
  try {
    const result = await http.delete('/user/unbind-wechat')
    return result
  } catch (error) {
    console.error('解绑微信失败:', error)
    throw error
  }
}

/**
 * 获取销售员列表
 */
export function getSalesmanList() {
  return request({
    url: '/salesman/list',
    method: 'get'
  })
}    